package com.szdvte.news.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.szdvte.news.bean.Comments;
import com.szdvte.news.bean.News;
import com.szdvte.news.bean.NewsFilmResource;
import com.szdvte.news.dao.ICommentsDao;
import com.szdvte.news.dao.INewsDao;
import com.szdvte.news.dao.INewsFilmResourceDao;
import com.szdvte.news.service.INewsService;
import com.szdvte.news.util.WiterUtil;

public class NewsServiceImpl implements INewsService {
	private INewsDao newsDao;
	private INewsFilmResourceDao newsFilmResourceDao;
	private ICommentsDao commentsDao;
	@Override
	public List<News> selectNews(News news) {
		return select(newsDao.selectNews(news));
	}

	@Override
	public News loadNews(News news) {
		news = this.newsDao.loadNews(news);
		news.setPhotos(pic(news.getNewsId()));
		news.setNewsParserData(WiterUtil.parseData(news.getNewsData()));
		List<Comments> list = commentsDao.selectComments(new Comments(news.getNewsId()));
		List<Comments> listNew = new ArrayList<Comments>();
		for(Comments comments : list){
			comments.setCommentsParserData(WiterUtil.parseData(comments.getCommentsData()));
			listNew.add(comments);
		}
		news.setComments(listNew);
		return news;
	}
	public INewsDao getNewsDao() {
		return newsDao;
	}
	public void setNewsDao(INewsDao newsDao) {
		this.newsDao = newsDao;
	}
	public INewsFilmResourceDao getNewsFilmResourceDao() {
		return newsFilmResourceDao;
	}
	public void setNewsFilmResourceDao(INewsFilmResourceDao newsFilmResourceDao) {
		this.newsFilmResourceDao = newsFilmResourceDao;
	}
	private List<NewsFilmResource> pic(Integer id){
		return newsFilmResourceDao.selectPic(new NewsFilmResource(id));
	}

	public ICommentsDao getCommentsDao() {
		return commentsDao;
	}

	public void setCommentsDao(ICommentsDao commentsDao) {
		this.commentsDao = commentsDao;
	}
	private List<News> select(List<News> listOldNews){
		
		List<News> listNewNews = new ArrayList<News>();
		for(News n:listOldNews){
			n.setPhotos(pic(n.getNewsId()));
			listNewNews.add(n);
		}
		return listNewNews;
	}
	@Override
	public List<News> comments(News news) {
		return select(newsDao.comments(news));
	}

	@Override
	public List<News> pic(News news) {
		return select(newsDao.pic(news));
	}

	@Override
	public boolean add(News news,String path) 
	{
		int id = newsDao.seq();
		NewsFilmResource filmResource = new NewsFilmResource();
		filmResource.setNewsFilmResourceNewsId(id);
		filmResource.setNewsFilmResourceType(" ");
		filmResource.setNewsFilmResourceUrl(path);
		news.setNewsId(id);
		if(newsFilmResourceDao.add(filmResource)){
			return newsDao.add(news);
		}else{
			return false;
		}
	}

	@Override
	public boolean update(News news) {
		
		return newsDao.update(news);
	}

	@Override
	public boolean delete(News news) {
	   boolean flag = newsFilmResourceDao.delete(new NewsFilmResource(news.getNewsId()));
	   if(flag){
		   if(commentsDao.delete(new Comments(news.getNewsId()))){
			   return newsDao.delete(news);
		   }else{
			   return false;
		   }
			
		}else{
			return false;
		}
	}

	@Override
	public List<News> select(News news) {
		return newsDao.select(news);
	}

	@Override
	public News load(News news) {
		news = newsDao.loadNews(news);
		List<Comments> list = new ArrayList<Comments>();
		List<Comments> list1 = commentsDao.selectAll(new Comments());
		for(Comments comments:list1){
			comments.setCommentsParserData(WiterUtil.parseData(comments.getCommentsData()));
			list.add(comments);
		}
		news.setComments(list);
		news.setPhotos(newsFilmResourceDao.select(new NewsFilmResource(news.getNewsId())));
		return news;
	}

	@Override
	public List<News> loadType(News news) {
		// TODO Auto-generated method stub
		return newsDao.loadType(news);
	}
}
